package com.heihei.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heihei.beans.LoadDatas;
import com.heihei.beans.PageBean;
import com.heihei.domain.User;
import com.heihei.domain.Websitesinfo;
import com.heihei.service.WebsitesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/sites")
public class WebsitesController {
    @Autowired
    private WebsitesService websitesService;

    ObjectMapper objectMapper = new ObjectMapper();

    //有条件查询（查询所有）
    @RequestMapping("/all")
    @ResponseBody
    public String findAllConditional(@RequestBody LoadDatas loadDatas, HttpSession session) {
        if(loadDatas.getPb() == null) {
            loadDatas.setPb(new PageBean());
        }

        if(loadDatas.getPb().getCurrentPage() == 0) {
            loadDatas.getPb().setCurrentPage(1);
        }

        //获取session中的登录用户User
        User currentUser = (User) session.getAttribute("currentUser");
/*        if(currentUser == null) {
            currentUser = new User();
            currentUser.setAuthority(0);
        }*/
        loadDatas.setUserAuthority(currentUser);

        //查询数据
        LoadDatas allSitesData = websitesService.findAllConditionally(loadDatas);

        String s = null;
        try {
            s = objectMapper.writeValueAsString(allSitesData);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

    //添加数据
    @RequestMapping("/add")
    @ResponseBody
    public String insertWebsiteInfo(@RequestBody Websitesinfo websitesinfo, HttpSession session){
        //先判断权限
        User currentUser = (User) session.getAttribute("currentUser");
        if(currentUser.getAuthority() != 1 && currentUser.getAuthority() != 3) {
            return "No";
        }

        websitesinfo.setUserid(currentUser.getId());
        //System.out.println(websitesinfo);
        //插入
        try {
            websitesService.insertWebsiteInfo(websitesinfo);
        } catch (Exception e) {
            e.printStackTrace();
            return "No";
        }

        return "OK";

    }

    //修改
    @RequestMapping("/edit")
    @ResponseBody
    public String editWebsiteInfo(@RequestBody Websitesinfo websitesinfo, HttpSession session){
        //先判断权限
        User currentUser = (User) session.getAttribute("currentUser");
        if(currentUser.getAuthority() != 1 && currentUser.getAuthority() != 3) {
            return "No";
        }

        websitesinfo.setEditTime(new Date());

        websitesService.updateWebsiteInfo(websitesinfo);

        return "Ok";

    }

    //根据id值回显数据
    @RequestMapping("/getWeb")
    @ResponseBody
    public String getWebsiteInfoById(@RequestParam Integer id, HttpSession session){
//        int id = website.getId();

        String str = "No";

        //先判断权限
        User currentUser = (User) session.getAttribute("currentUser");
        if(currentUser.getAuthority() != 1 && currentUser.getAuthority() != 3) {
            return str;
        }

        Websitesinfo websiteInfoById = websitesService.getWebsiteInfoById(id);
        if(websiteInfoById != null) {
            try {
                str = objectMapper.writeValueAsString(websiteInfoById);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        return str;
    }

    //根据id值回显数据
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteById(@RequestParam Integer id, HttpSession session){
        //先判断权限
        User currentUser = (User) session.getAttribute("currentUser");
        if(currentUser.getAuthority() != 1 && currentUser.getAuthority() != 3) {
            return "No";
        }

        try {
            websitesService.deleteWebsiteInfoById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "No";
        }
        return "Ok";
    }

}
